package com.masiyi.springai.controller;/*
 * Copyright 2023-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import com.masiyi.springai.service.TongYiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Map;

/**
 * TongYi models Spring Cloud Alibaba Controller.
 *
 * @author yuluo
 * @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
 * @since 2023.0.0.0
 */

@RestController
@RequestMapping("/ai")
@CrossOrigin
public class TongYiController {

    @Autowired
    @Qualifier("tongYiSimpleServiceImpl")
    private TongYiService tongYiSimpleService;

    @GetMapping("/example")
    public String completion(
            @RequestParam(value = "message", defaultValue = "Tell me a joke")
            String message
    ) {

        return tongYiSimpleService.completion(message);
    }

    @GetMapping("/stream")
    public Map<String, String> streamCompletion(
            @RequestParam(value = "message", defaultValue = "请告诉我西红柿炖牛腩怎么做？")
            String message
    ) {

        return tongYiSimpleService.streamCompletion(message);
    }


    @PostMapping(value = "/flux-demo", produces = "text/event-stream; charset=utf-8")
    public Flux<ServerSentEvent> hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        return Flux.interval(Duration.ofSeconds(1))  // 每隔一秒发送一个事件
                .take(10)  // 限制发送 10 次
                .map(seq -> {
                    String message = String.format("Hello %s, here is your message #%d at %s",
                            name, seq + 1, LocalTime.now());
                    return ServerSentEvent.<String>builder()
                            .id(String.valueOf(seq)) // 设置事件 ID
                            .event("chat-message")  // 自定义事件名称
                            .data(message)          // 传递的消息数据
                            .build();
                });
    }

    @PostMapping(value = "/flux", produces = "text/event-stream; charset=utf-8")
    public Flux<ServerSentEvent> flux(@RequestParam(name = "message", defaultValue = "你好") String message) {
        return tongYiSimpleService.flux(message);
    }


}
